/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package DataModels;


import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author TOSHIBA
 */
public class EmpFileOp {

    public final ArrayList<EmployeeData> allUsers = new ArrayList<EmployeeData>();

    public ArrayList<EmployeeData> readFromFile() {
        try {
            FileInputStream fis = new FileInputStream("C:\\Users\\TOSHIBA\\Desktop\\EmployeeDataDB.txt");
            InputStreamReader isr = new InputStreamReader(fis);
            BufferedReader br = new BufferedReader(isr);

            String line = "";
            ArrayList<EmployeeData> myUsers = new ArrayList<EmployeeData>();

            while ((line = br.readLine()) != null) {
                myUsers.add(getUserFromLine(line));
            }
            br.close();
            return myUsers;

        } catch (Exception ex) {
            Logger.getLogger(FileOp.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }

    }

    private EmployeeData getUserFromLine(String line) {
        EmployeeData myUser = new EmployeeData();

        String[] userData = line.split("#");
        myUser.setName(userData[0]);
        myUser.setID(Integer.parseInt(userData[1]));
        myUser.setDOB(userData[2]);
        myUser.setGender(userData[3]);
        myUser.setEmail(userData[4]);
        myUser.setSalary(Double.parseDouble(userData[5]));

        return myUser;
    }

    public boolean writeToFile(String userData) {
        try {
            FileOutputStream fos = new FileOutputStream("C:\\Users\\TOSHIBA\\Desktop\\EmployeeDataDB.txt", true);
            OutputStreamWriter osw = new OutputStreamWriter(fos);
            PrintWriter pw = new PrintWriter(osw);

            pw.println(userData);
            pw.flush();
            pw.close();

            return true;

        } catch (Exception e) {
            System.out.println("Can't write to the located file");
            return false;
        }
    }

    public boolean deleteProfile(int userindex) {

        allUsers.addAll(readFromFile());

        allUsers.remove(userindex);
        try {
            FileOutputStream fis = new FileOutputStream("C:\\Users\\TOSHIBA\\Desktop\\EmployeeDataDB.txt", false);
            OutputStreamWriter oiw = new OutputStreamWriter(fis);
            PrintWriter piw = new PrintWriter(oiw);

            for (int i = 0; i < allUsers.size(); i++) {

                piw.println(allUsers.get(i).getName() + "#" + allUsers.get(i).getID() + "#" + allUsers.get(i).getDOB() + "#" + allUsers.get(i).getGender() + "#" + allUsers.get(i).getEmail() + "#" + allUsers.get(i).getSalary());
                piw.flush();
            }

            piw.close();

            return true;

        } catch (Exception e) {
            System.out.println("Can't write to the located file");
            return false;

        }

    }

    public boolean updateProfile(EmployeeData user, int userindex) {

        allUsers.addAll(readFromFile());

        allUsers.set(userindex, user);
        try {
            FileOutputStream fis = new FileOutputStream("C:\\Users\\TOSHIBA\\Desktop\\EmployeeDataDB.txt", false);
            OutputStreamWriter oiw = new OutputStreamWriter(fis);
            PrintWriter piw = new PrintWriter(oiw);

            for (int i = 0; i < allUsers.size(); i++) {

                piw.println(allUsers.get(i).getName() + "#" + allUsers.get(i).getID() + "#" + allUsers.get(i).getDOB() + "#" + allUsers.get(i).getGender() + "#" + allUsers.get(i).getEmail() + "#" + allUsers.get(i).getSalary());
                piw.flush();
            }

            piw.close();

            return true;

        } catch (Exception e) {
            System.out.println("Can't write to the located file");
            return false;

        }

    }

}
